<% if false # the license inside this if block assertains to this file -%>
# Copyright 2017 Google Inc.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
<% end -%>
<%# Emits network mocks specifically designed for each resourceref -%>
<%# Requires: none -%>
<% object.all_resourcerefs.each do |ref| -%>
<%
  prop_ref = ref.resource_ref
  ref_underscore_name = Google::StringUtils.underscore(ref.resource)
  ref_index ||= 0
  ref_name = "resource(#{ref_underscore_name},#{ref_index})"
-%>
def expect_network_get_success_<%= ref_underscore_name -%>(id, data = {})
  id_data = data.fetch(:name, '').include?('title') ? 'title' : 'name'
  body = load_network_result_<%= ref_underscore_name -%>("success#{id}~" \
                                                         "#{id_data}.yaml")
         .to_json
  uri = uri_data_<%= ref_underscore_name -%>(id).merge(data)

  request = double('request')
  allow(request).to receive(:send).and_return(http_success(body))

  debug_network "!! GET #{uri}"
  expect(Google::<%= product_ns -%>::Network::Get).to receive(:new)
    .with(self_link_<%= ref_underscore_name -%>(uri),
          instance_of(Google::FakeAuthorization)) do |args|
    debug_network ">> GET #{args}"
    request
  end
end

def load_network_result_<%= ref_underscore_name -%>(file)
  results = File.join(File.dirname(__FILE__), 'data', 'network',
                      '<%= prop_ref.out_name -%>', file)
  raise "Network result data file #{results}" unless File.exist?(results)
  data = YAML.safe_load(File.read(results))
  raise "Invalid network results #{results}" unless data.class <= Hash
  data
end

<% if @constants.value_assign(prop_ref).empty? -%>
def uri_data_<%= ref_underscore_name -%>(_id)
  {}
end

<% else # value.keys.empty? -%>
<%# We can call any variable because all specs are loaded at once -%>
<%# Also, all value holders are global -%>
# Creates variable test data to comply with self_link URI parameters
# Only used for <%= prop_ref.out_name -%> objects
def uri_data_<%= ref_underscore_name -%>(id)
  {
<%= lines(indent_list(@constants.value_assign(prop_ref), 4)) -%>
  }
end

<% end # value.keys.empty? -%>
<%
  self_link_ref = emit_link("self_link_#{ref_underscore_name}",
                            self_link_url(prop_ref),
                            false)
  self_link_ref = self_link_ref.gsub("expand_variables",
                                     "expand_variables_#{ref_underscore_name}")
-%>
<%= lines(self_link_ref, 1) -%>
<% end -%>
